<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mzone extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 
     * @void Country
     * 
     */
    
    public function getZone()
    {
        $sql = "SELECT id,title as name FROM zone";
        $data = $this->db->query($sql)->result_array();
        return $data;
        
    }
    
    public function getZoneSlide($slide_id){
        $sql = "SELECT DISTINCT zone_id as id FROM slide_zone WHERE slide_id = $slide_id Order by zone_id ASC" ;
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
    
    
    public function getZoneCountryDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT * FROM country WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (title like '%%".$key."%%' OR ";
            $sql .= " title_ascii like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }
    
    public function addCountryDb($admin_id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->insert('country', $arr);
        return 200;
    }
    public function editCountryDb($admin_id, $id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->update('country', $arr, "id = ".$id);
        return 200;
    }
    
    public function deleteCountryDB($id)
    {
        $this->db->query("DELETE FROM country WHERE id IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')");
        return 200;
    }
    
    public function getCountryDB($id)
    {
        return $this->db->query('SELECT * FROM country WHERE id = '.$id)->row_object();
    }
    
    
    /**
     * 
     * @void Country
     * 
     */
    
    public function optionCountry($id)
    {
        $data = $this->db->query('SELECT id, title FROM country ORDER BY title ASC')->result_object();
        $html = '';
        
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
    public function getZoneDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT z.title, z.title_ascii, z.created, z.id, c.title AS ctitle, c.title_ascii AS ctitle_ascii FROM zone AS z INNER JOIN country AS c ON z.id_country = c.id WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (z.title like '%%".$key."%%' OR ";
            $sql .= " c.like like '%%".$key."%%' OR ";
            $sql .= " c.title_ascii like '%%".$key."%%' OR ";
            $sql .= " z.title_ascii like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY z.orderby ASC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }
    
    public function addZoneDb($admin_id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'id_country'    => $this->getParamInt('country'),
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->insert('zone', $arr);
        return 200;
    }
    public function editZoneDb($admin_id, $id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'id_country'    => $this->getParamInt('country'),
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->update('zone', $arr, "id = ".$id);
        return 200;
    }
    
    public function deleteZoneDB($id)
    {
        $this->db->query("DELETE FROM zone WHERE id IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')");
        return 200;
    }
    
    public function getZoneDetailDB($id)
    {
        return $this->db->query('SELECT * FROM zone WHERE id = '.$id)->row_object();
    }
	
    public function sortZoneDb($arr)
    {
        $exp = explode(',' , substr($arr, 0, -1));
        foreach($exp AS $el=>$le)
        {
            $this->db->update('zone', array('orderby' => ($el+1)), "id = ".$le);
            //$this->db->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        }
        return 'ok';
    }
}
